<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/3/15
 * Time: 14:10
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\AlbumTemplateGoodsModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class AlbumTemplateGoodsService
 * @package SDXApi\Service
 */
class AlbumTemplateGoodsService extends TemplateAbstract
{
	/**
	 * @var AlbumTemplateGoodsModel
	 */
	protected $albumTemplateGoodsModel;

	/**
	 * @var GoodsRepository
	 */
	protected $goodsRepository;

	/**
	 * AlbumTemplateGoodsService constructor.
	 */
	public function __construct()
	{
		$this->albumTemplateGoodsModel = new AlbumTemplateGoodsModel();
		$this->goodsRepository = new GoodsRepository();
	}

	/**
	 * 处理商品类型
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
//		$userRank = $this->getUserRank($userId);
//		$cacheName = 'album_template_goods_id_' . $template['id'] . '_rank_' . $userRank . '_wid_' . $wid;
//		$data = $this->getCache($cacheName);
//
//		if (empty($data)) {
//			$data = $this->checkGoods($template, $userId, $wid);
//			$this->cacheData($cacheName, $data, mt_rand(3600, 4200));
//		}
		$data = $this->checkGoods($template, $userId, $wid);

		return $data;
	}


	/**
	 * 处理商品
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	protected function checkGoods($template = array(), $userId = 0, $wid = 8)
	{
		if (empty($template)) {
			return false;
		}

		$data = $this->checkGoodsTransform($template['id'], $userId, $wid);

		if (empty($data)) {
			$data = array();
		}

		foreach ($data as $key => $value) {
			$data[$key]['id'] = $value['goods_id'];
		}

		$template['goods_list'] = $data;

		return $template;
	}

	/**
	 * 处理商品数据
	 * Created by Jerry
	 * @param int $id
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	protected function checkGoodsTransform($id = 0, $userId = 0, $wid = 8)
	{
		if (empty($id) || empty($wid)) {
			return false;
		}

		$userRank = $this->getUserRank($userId);
		$cacheName = 'album_template_goods_id_' . $id . '_rank_' . $userRank . '_wid_' . $wid;
		$goodsData = $this->getCache($cacheName);

		$this->goodsRepository->userId = $userId;
		$this->goodsRepository->wid = $wid;

		if (empty($goodsData)) {
			$goods = $this->albumTemplateGoodsModel->findAlbumGoodsInfoByAlbumId($id, $wid);

			if (empty($goods)) {
				return false;
			}

			$goodsData = $this->goodsRepository->formatGoodsTransform($goods);

			if (! empty($goodsData)) {
				$this->cacheData($cacheName, $goodsData, mt_rand(3600, 4200));
			}
			else{
				$goodsData = array();
			}

		}

		if (count($goodsData) > 1) {
			$goodsData = self::checkDataSort($goodsData);
		}

		return $this->goodsRepository->checkGoodsPractical($goodsData);
	}

	/**
	 * 排序
	 * Created by Jerry
	 * @param $data
	 * @param string $sortKey
	 * @return mixed
	 */
	protected static function checkDataSort($data, $sortKey = 'sort')
	{
		$sort = array();
		foreach ($data as $key => $value) {
			$sort[] = $value[$sortKey];
		}

		array_multisort($sort, SORT_DESC, $data);

		return $data;
	}


}